define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'users/index',
                    add_url: 'users/add',
                    edit_url: 'users/edit',
                    del_url: 'users/del',
                    multi_url: 'users/multi',
                    table: 'users',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                columns: [
                    [
                        {field: 'user_id', title: __('User_id')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'avatar', title: __('Avatar'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'nick_name', title: __('Nick_name'), operate: 'like', },
                        {field: 'score', title: __('Score'), operate: false},
                        {field: 'scort_sum', title: __('Scort_sum'), operate: false},
                        {field: 'balance', title: __('Balance'), operate: false},
                        {field: 'recharge_sum', title: __('Recharge_sum'), operate: false},
                        {field: 'consume_sum', title: __('Consume_sum'), operate: false},
                        {field: 'status', title: __('Status'), visible: false, searchList: {"0": __('Status 0'), "1": __('Status 1')}},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        }
                    ]
                ]
            });

            //分配用户
            $(document).on("click", ".btn-xq", function () {
                var ids = $(this).data("id");
                var url = $(this).data("url");
                var title = $(this).data("title");
                Backend.api.open(url + '/ids/' + ids, title, {area: ["90%", "90%"]}, {
                    callback: function (value) {
                        table.bootstrapTable('refresh', {});
                    },
                });

            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        }, detail: function () {
            $(document).on("click", ".btn-setwallet", function (e) {
                layer.open({
                    type: 1,
                    title: '调整钱包金额',
                    maxmin: true,
                    area: ['600px', '400px'],
                    content: Template('setwallet-user', {}),
                    end: function () {
                    }
                });
            });
            $(document).on("click", ".btn-setscore", function (e) {
                layer.open({
                    type: 1,
                    title: '调整积分数量',
                    maxmin: true,
                    area: ['600px', '400px'],
                    content: Template('setscore-user', {}),
                    end: function () {
                    }
                });
            });
            Table.api.init();
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.detail_table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".icon-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        detail_table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                var wallet_id = table1.attr('wallet_id');
                table1.bootstrapTable({
                    url: 'users/getWalletList',
                    pk: 'wallet_log_id',
                    sortName: 'wallet_log_id',
                    toolbar: '#toolbar1',
                    search: false,
                    showToggle: false,
                    showRefresh: true,
                    showColumns: false,
                    showExport: false,
                    showHeader: false,
                    queryParams: function (params) {
                        var filter = params.filter ? JSON.parse(params.filter) : {};
                        filter.wallet_id = wallet_id;
                        params.filter = JSON.stringify(filter);
                        return params;
                    },
                    columns: [
                        [
                            {field: 'left', formatter: Controller.api.formatter.wallet_left},
                            {field: 'right', formatter: Controller.api.formatter.wallet_right},
                            {field: 'createtime', title: __('变动时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', visible: false},
                        ]
                    ]
                });
                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                var user_id = table2.attr('user_id');
                table2.bootstrapTable({
                    url: 'users/getScoreList',
                    pk: 'score_log_id',
                    sortName: 'score_log_id',
                    toolbar: '#toolbar2',
                    search: false,
                    showToggle: false,
                    showRefresh: true,
                    showColumns: false,
                    showExport: false,
                    showHeader: false,
                    queryParams: function (params) {
                        var filter = params.filter ? JSON.parse(params.filter) : {};
                        filter.user_id = user_id;
                        params.filter = JSON.stringify(filter);
                        return params;
                    },
                    columns: [
                        [
                            {field: 'left', formatter: Controller.api.formatter.wallet_left},
                            {field: 'right', formatter: Controller.api.formatter.score_right},
                            {field: 'createtime', title: __('变动时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', visible: false},
                        ]
                    ]
                });
                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
            third: function () {
                // 表格3
                var table3 = $("#table3");
                var user_id = table3.attr('user_id');
                table3.bootstrapTable({
                    url: 'order/index',
                    pk: 'order_id',
                    sortName: 'order_id',
                    toolbar: '#toolbar3',
                    search: false,
                    showToggle: false,
                    showRefresh: true,
                    showColumns: false,
                    showExport: false,
                    showHeader: true,
                    queryParams: function (params) {
                        var filter = params.filter ? JSON.parse(params.filter) : {};
                        filter.user_id = user_id;
                        filter.type = 1;
                        params.filter = JSON.stringify(filter);
                        return params;
                    },
                    columns: [
                        [
                            {field: 'order_no', title: __('订单号'), operate: false},
                            {field: 'box_no', title: __('盒子编号'), operate: false},
                            {field: 'goods_price', title: __('商品金额'), operate: false},
                            {field: 'pay_price', title: __('实付金额'), operate: false},
                            {field: 'pay_type', title: __('支付方式'), visible: false, operate: false},
                            {field: 'pay_type_text', title: __('支付方式'), operate: false},
                            {field: 'status', title: __('订单状态'), visible: false, searchList: {"0": __('待支付'), "10": __('已完成'), "20": __('已取消')}},
                            {field: 'status_text', title: __('订单状态'), operate: false},
                            {field: 'createtime', title: __('下单时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                            //自定义栏位,custom是不存在的字段
                            {
                                field: 'order',
                                title: __('操作'),
                                operate: false,
                                formatter: Controller.api.formatter.order
                            }
                        ]
                    ]
                });
                // 为表格2绑定事件
                Table.api.bindevent(table3);
            },
            four: function () {
                // 表格4
                var table4 = $("#table4");
                var user_id = table4.attr('user_id');
                table4.bootstrapTable({
                    url: 'users/getGoodsList?ids='+user_id,
                    pk: 'goods_id',
                    sortName: 'goods_id',
                    toolbar: '#toolbar4',
                    search: false,
                    showToggle: false,
                    showRefresh: true,
                    showColumns: false,
                    showExport: false,
                    showHeader: true,
                    columns: [
                        [
                            {field: 'no', title: __('序号'), operate: false},
                            {field: 'goods_title', title: __('商品名称'), operate: false},
                            {field: 'brand_title', title: __('品牌'), operate: false},
                            {field: 'cate_title', title: __('分类'), operate: false},
                            {field: 'buy_num',sortable: true, title: __('购买数量'), operate: false},
                            {field: 'total_price',sortable: true, title: __('消费金额'), operate: false},
                            {field: 'createtime', title: __('下单时间'), visible: false, operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                            //自定义栏位,custom是不存在的字段
                            {
                                field: 'order',
                                title: __('操作'),
                                operate: false,
                                formatter: Controller.api.formatter.goods
                            }

                        ]
                    ]
                });
                // 为表格2绑定事件
                Table.api.bindevent(table4);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var btn = row.status == '0' ? '<b style="color: #f90f0f;">禁用</b>' : '<b style="color: #72afd2;">启用</b>';
                    var html = '<a class="btn-change text-success" style="cursor:pointer" data-url="users/updatestatus" data-id="' + row.user_id + '">' + btn + '</a>';
                    html += '<a href="javascript:;" data-url="users/detail" class="btn-xq text-succes" data-title="用户信息" data-id="' + row.user_id + '" style="margin-left: 10px;" ><b>查看</b></a>';
                    return html;
                },
                order: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a href="order/detail/ids/' + row.order_id + '" class="btn-change text-success btn-addtabs" style="margin-left: 10px;" title="订单详情" data-table-id="table" data-field-index="11" data-row-index="0" data-button-index="2"><b>订单详情</b></a>';
                    return html;
                },
                wallet_left: function (value, row, index) {
                    var html = row.change_matters_text;
                    if (row.order_id != 0) {
                        html += "(单号:" + row.order_no + ")";
                    }
                    html += "<br/>" + row.createtime;
                    return html;
                },
                wallet_right: function (value, row, index) {
                    var html = '';
                    if (row.change_type == 0) {
                        html += '+';
                    } else {
                        html += '-';
                    }
                    html += "¥" + row.change_amount;
                    if (row.remark != null) {
                        html += "<br/>" + row.remark;
                    }
                    return html;
                },
                score_right: function (value, row, index) {
                    var html = '';
                    if (row.change_type == 0) {
                        html += '+';
                    } else {
                        html += '-';
                    }
                    html += row.change_amount;
                    if (row.remark != null) {
                        html += "<br/>" + row.remark;
                    }
                    return html;
                },
                goods: function (value, row, index) {

                    var html = '<a href="javascript:;" class="text-info btn-xq" data-title="走势图" data-id="' + row.box_goods_id + '" data-url="reportbox/orderdetail">走势图</a>';
                    return html;
                },
            }
        }
    };
    return Controller;
});